package com.gewu.pm.ui.activity.approval;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.fragment.approval.MyApplicationFragment;
import com.gewu.pm.ui.fragment.approval.MyApprovalFragment;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.base.FragmentPagerAdapter;

import java.util.Arrays;
import java.util.List;

/**
 * 我的申请与审批主Activity
 */
public final class ApprovalApplicationActivity extends AppActivity
        implements ViewPager.OnPageChangeListener {

    private XTabLayout mTabLayout;
    private TextView mFilterAllView;
    private TextView mTimeRangeView;
    private ViewPager mViewPager;

    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    private MyApprovalFragment mMyApprovalFragment;
    private MyApplicationFragment mMyApplicationFragment;

    private int mCurrentFilterType = 0;  // 当前筛选类型（0=全部，1-4=状态）
    private int mCurrentApprovalType = 0;  // 当前审批类型（0=全部审批）

    public static void start(Context context) {
        Intent intent = new Intent(context, ApprovalApplicationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.approval_application_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTabLayout = findViewById(R.id.tab_layout);
        mFilterAllView = findViewById(R.id.tv_filter);
        mTimeRangeView = findViewById(R.id.tv_date_range);
        mViewPager = findViewById(R.id.vp_content);

        // 创建 Fragment
        mMyApprovalFragment = MyApprovalFragment.newInstance();
        mMyApplicationFragment = MyApplicationFragment.newInstance();

        // 设置 ViewPager
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(mMyApprovalFragment, "我的审批");
        mPagerAdapter.addFragment(mMyApplicationFragment, "我的申请");
        mViewPager.setAdapter(mPagerAdapter);

        // 关联 XTabLayout 和 ViewPager
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(this);

        // 设置点击事件
        setOnClickListener(R.id.ll_date_filter,R.id.tv_date_range);
    }

    @Override
    protected void initData() {
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.ll_date_filter) {
            // 全部筛选弹窗
            showFilterPopup();
        } else if (id == R.id.tv_date_range) {
            // 时间范围选择
            toast("时间范围选择");
            // TODO: 打开时间选择器
        }
    }

    /**
     * 显示状态筛选弹窗
     */
    private void showFilterPopup() {
        List<String> filterList;

        if (mViewPager.getCurrentItem() == 0) {
            // 我的审批：全部、待审批、已通过、已驳回
            filterList = Arrays.asList("全部", "待审批", "已通过", "已驳回");
        } else {
            // 我的申请：全部、审批中、已通过、已驳回、已撤销
            filterList = Arrays.asList("全部", "审批中", "已通过", "已驳回", "已撤销");
        }

        SelectTimePopup.Builder builder = new SelectTimePopup.Builder(this)
                .setList(filterList)
                .setSelectedPosition(mCurrentFilterType)
                .setListener((popupWindow, category) -> {
                    mFilterAllView.setText(category);

                    // 更新筛选类型
                    mCurrentFilterType = filterList.indexOf(category);

                    // 通知 Fragment 更新数据
                    if (mViewPager.getCurrentItem() == 0) {
                        mMyApprovalFragment.updateFilter(mCurrentFilterType);
                    } else {
                        mMyApplicationFragment.updateFilter(mCurrentFilterType, mCurrentApprovalType);
                    }
                });

        builder.showAsDropDown(mFilterAllView);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        // 重置筛选
        mFilterAllView.setText("全部");
        mCurrentFilterType = 0;
        mCurrentApprovalType = 0;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}

